package com.wyl.studyredis.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.studyredis.business.entity.Car;
import com.wyl.studyredis.business.mapper.CarMapper;
import com.wyl.studyredis.business.service.ICarService;
import com.wyl.studyredis.business.vo.CarVo;
import com.wyl.studyredis.framework.util._BeanUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyl
 * @since 2022-02-13
 */
@Service("carService")
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @CachePut(cacheNames = "car", key = "#result.id")
    @Override
    public CarVo saveAndCache(Car car) throws Exception {
        super.save(car);
        return _BeanUtil.copyProperties(car, CarVo.class);
    }

    @Cacheable(cacheNames = "car", key = "#id", unless = "null == #result")
    @Override
    public CarVo getByIdAndCache(String id) throws Exception {
        Car car = super.getById(id);
        return _BeanUtil.copyProperties(car, CarVo.class);
    }

    @CachePut(cacheNames = "car", key = "#car.id")
    @Override
    public CarVo updateByIdAndCache(Car car) throws Exception {
        super.updateById(car);
        return _BeanUtil.copyProperties(car, CarVo.class);
    }

    @CacheEvict(cacheNames = "car", key = "#id", allEntries = true)
    @Override
    public void removeByIdAndCache(String id) {
        super.removeById(id);
    }

}























